package com.test.qnews.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.qnews.R;
import com.test.qnews.beans.GridBeans;

import java.util.List;

/**
 * Created by dllo on 16/1/17.
 */
public class GridViewAdapter extends BaseAdapter {

    //    private String mTitleArr[] = null;
//    private int mImgIdArr[] = null;
    private LayoutInflater inflater = null;
    private List<GridBeans> data;


    public GridViewAdapter(Context context, List<GridBeans> data) {
        super();
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    /**
     * 获取Item总数
     */
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    /**
     * 获取Item
     */
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    /**
     * 获取ItemID
     */
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.item_grid,parent,false);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.gridview_text);
            holder.image = (ImageView) convertView.findViewById(R.id.gridview_img);
            holder.time = (TextView) convertView.findViewById(R.id.gridview_time);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();

        }

        // 设置内容
        holder.title.setText(data.get(position).getTitle());
        holder.image.setBackgroundResource(data.get(position).getImage());
        holder.time.setText(data.get(position).getTime());

        return convertView;
    }

    /**
     * 工具类
     */
    private class ViewHolder {
        TextView title = null;
        ImageView image = null;
        TextView time = null;
    }

}


